﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;

namespace blackhouse.BlackApart.Business.Prices {
    public class DaApartmentPrice : IApartmentPrice {

        #region Fields

        private int apartmentId;
        private IPricesDataAccess dataAccess;
        private ImpApartmentPrice instance;

        #endregion

        #region Properties

        public int ReservationNumberOfPeoples {
            get { return this.instance.ReservationNumberOfPeoples; }
            set { this.instance.ReservationNumberOfPeoples = value; }
        }

        public DateTime ReservationFromDay {
            get { return this.instance.ReservationFromDay; }
            set { this.instance.ReservationFromDay = value; }
        }

        public DateTime ReservationToDay {
            get { return this.instance.ReservationToDay; }
            set { this.instance.ReservationToDay = value; }
        }

        #endregion

        #region Constructors

        public DaApartmentPrice(int apartmentId, IPricesDataAccess iPricesDataAccess) {
            this.apartmentId = apartmentId;
            this.dataAccess = iPricesDataAccess;
            this.instance = new ImpApartmentPrice();
            this.instance.InitialPrice = this.dataAccess.GetApartmentPrice(this.apartmentId);
            this.instance.SetModifiers(this.ReadModifiers(this.apartmentId));
        }

        #endregion

        #region Methods

        private List<ApartmentPriceModifier> ReadModifiers(int apartmentId) {
            ApartmentPriceData[] data = this.dataAccess.GetPriceModifiers(apartmentId);
            List<ApartmentPriceModifier> modifiers = new List<ApartmentPriceModifier>(data.Length);
            for (int xd = 0; xd < data.Length; xd++) {
                ApartmentPriceModifier modifier = this.InitiateInstance(data[xd]);
                if (modifier != null)
                    modifiers.Add(modifier);
            }
            return modifiers;
        }

        public ApartmentPriceModifier InitiateInstance(ApartmentPriceData data) {
            Type t = Type.GetType(data.ClassDeclaration, false);
            if (t == null) return null;
            ApartmentPriceModifier modifier = Activator.CreateInstance(t) as ApartmentPriceModifier;
            if (modifier == null) return null;

            modifier.ModificationFromVariable = data.ValueFrom;
            modifier.ModificationToVariable = data.ValueTo;
            modifier.ModificationFixedVariable = data.ValueEquals;
            modifier.ModificationType = (PriceModificationType) Enum.Parse(typeof(PriceModificationType), data.ChangeType);
            modifier.ModificationChangeValue = data.ChangeValue;

            return modifier;
        }

        public double GetPrice() {
            return this.instance.GetPrice();
        }

        public double GetPriceWithoutRabat() {
            return this.instance.GetPriceWithoutRabat();
        }

        public float GetRabat()
        {
            return this.instance.GetRabat();
        }

        #endregion

    }
}
